package com.swhysc.information.controller;


import com.swhysc.information.common.model.GridView;
import com.swhysc.information.common.model.ResultBody;
import com.swhysc.information.common.model.ResultStatus;
import com.swhysc.information.entity.InformationDataDirectoryEx;
import com.swhysc.information.service.IInformationDataDirExService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @Author zmq
 * @Date 2022/10/13 17:47
 * @Description 资讯数据目录
 */
@RestController
@RequestMapping("/api/infor/data/dir/experiment")
@Slf4j
@Api(value = "InformationDataDirExController", tags = "资讯数据目录(实验室)")
public class InformationDataDirExController {
    @Autowired
    private IInformationDataDirExService iInformationDataDirService;


    @ApiOperation(value="新增", notes = "新增目录信息")
    @PostMapping("/create")
    public ResultBody create(@RequestBody InformationDataDirectoryEx informationDataDirectory){
        try {
            return ResultBody.success(iInformationDataDirService.create(informationDataDirectory));
        }catch (RuntimeException e){
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    @ApiOperation(value="上传数据源图片", notes = "上传数据源图片")
    @PostMapping("/uploadImage")
    public ResultBody uploadImage(@RequestBody MultipartFile multipartFile){
        try {
            return ResultBody.success(iInformationDataDirService.uploadImage(multipartFile));
        }catch (RuntimeException e){
            return ResultBody.failure(ResultStatus.DATA_OPERATION_ERROR, e);
        }
    }

    /**
     * 目录树列表接口
     *
     * @param name 目录名称
     * @param code 目录编码
     * @param page 页数
     * @param rows 行数
     * @param id  id
     * @return ResultBody
     */
    @GetMapping("/list")
    @ApiOperation(value = "目录树列表接口", notes = "目录树列表接口")
    public ResultBody<GridView> list(
            @ApiParam("类型区分 1 数据源 2 目录") @RequestParam Integer type,
            @ApiParam("目录名称") @RequestParam(required = false) String name,
            @ApiParam("目录编码") @RequestParam(required = false) String code,
            @ApiParam("id") @RequestParam(required = false) String id,
            @ApiParam("页数") @RequestParam Integer page,
            @ApiParam("每页条数") @RequestParam Integer rows) {
        try {
            List<InformationDataDirectoryEx> infoList = iInformationDataDirService.list(type,name, code, id, page, rows);
            int cnt = iInformationDataDirService.count(type,name, code, id);
            return ResultBody.success(new GridView(infoList, cnt));
        } catch (RuntimeException e) {
            log.error("【目录树列表接口异常】:{}",e.getMessage());
            return ResultBody.failure("【目录树列表接口异常】");
        }
    }

    /**
     * 目录树列表接口
     *
     * @param name 目录名称
     * @param code 目录编码
     * @param page 页数
     * @param rows 行数
     * @param id  id
     * @return ResultBody
     */
    @GetMapping("/allList")
    @ApiOperation(value = "当前目录下所有目录列表接口", notes = "当前目录下所有目录列表接口")
    public ResultBody<GridView> allList(
            @ApiParam("类型区分 1 数据源 2 目录") @RequestParam Integer type,
            @ApiParam("目录名称") @RequestParam(required = false) String name,
            @ApiParam("目录编码") @RequestParam(required = false) String code,
            @ApiParam("id") @RequestParam(required = false) String id,
            @ApiParam("页数") @RequestParam Integer page,
            @ApiParam("每页条数") @RequestParam Integer rows) {
        try {
            List<InformationDataDirectoryEx> infoList = iInformationDataDirService.allList(type,name, code, id, page, rows);
            int cnt = iInformationDataDirService.allCount(type,name, code, id);
            return ResultBody.success(new GridView(infoList, cnt));
        } catch (RuntimeException e) {
            log.error("【目录树列表接口异常】:{}",e.getMessage());
            return ResultBody.failure("【目录树列表接口异常】");
        }
    }


    /**
     * 编辑
     * @param  infoDataDirectory 目录实体
     * @return ResultBody
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新树列表接口", notes = "更新树列表接口")
    public ResultBody update(@RequestBody InformationDataDirectoryEx infoDataDirectory) {
        try {
            return ResultBody.success(iInformationDataDirService.update(infoDataDirectory));
        } catch (Exception e) {
            log.error("【目录树更新接口异常】:{}",e.getMessage());
            return ResultBody.failure("【目录树更新接口异常】" + e.getMessage());
        }
    }


    /**
     * 资讯目录、数据源目录批量删除
     *
     * @param ids  id集合
     * @param type 类型
     * @return ResultBody
     * 删除-》有关联表信息的话不能删除   有下级节点的话不能删除
     */
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "资讯目录、数据源目录批量删除", notes = "资讯目录、数据源目录批量删除")
    public ResultBody deleteBatch(@ApiParam(value = "批量删除id集合") @RequestParam String ids,
                                  @ApiParam(value = "类型") @RequestParam Integer type) {
        try {
            return ResultBody.success(iInformationDataDirService.deleteBatch(Arrays.asList(ids.split(",")), type));
        } catch (Exception e) {
            log.error("【资讯目录、数据源目录批量删除接口异常】:{}", e.getMessage());
            return ResultBody.failure("【资讯目录、数据源目录批量删除接口异常】");
        }
    }

    /**
     * 根据id获取目录树形结构
     * @param id id
     * @param name 名称
     * @return ResultBody
     */
    @GetMapping("/tree")
    @ApiOperation(value = "获取树形结构", notes = "获取树形结构")
    public ResultBody getTree(
            @ApiParam(value = "id") @RequestParam(required = false) String id,
            @ApiParam(value = "名称") @RequestParam(required = false) String name) {
        try {
            return ResultBody.success(iInformationDataDirService.getTreeById(id,name));
        } catch (Exception e) {
            log.error("【获取树形结构接口异常】:{}",e.getMessage());
            return ResultBody.failure("获取树形结构接口异常");
        }
    }

    /**
     * 根据id获取目录树形结构
     * @param id id
     * @return ResultBody
     */
    @GetMapping("/allTree")
    @ApiOperation(value = "获取树形结构", notes = "获取树形结构")
    public ResultBody getTree(
            @ApiParam(value = "id") @RequestParam(required = false) String id) {
        try {
            return ResultBody.success(iInformationDataDirService.getAllTree(id));
        } catch (Exception e) {
            log.error("【获取树形结构接口异常】:{}",e.getMessage());
            return ResultBody.failure("获取树形结构接口异常");
        }
    }

    /**
     * 根据id获取数据
     * @param id 主键id
     * @param type 类型
     * @return
     */
    @GetMapping("/view")
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    public ResultBody view(
            @ApiParam(value = "id") @RequestParam String id,
            @ApiParam(value = "类型 1 数据源 2 目录") @RequestParam Integer type) {
        try {
            return ResultBody.success(iInformationDataDirService.view(id,type));
        } catch (Exception e) {
            log.error("【获取树形结构接口异常】:{}",e.getMessage());
            return ResultBody.failure("获取树形结构接口异常");
        }
    }

    /**
     * 格式化levelCode
     * @return
     */
    @GetMapping("/format")
    @ApiOperation(value = "格式化levelCode", notes = "格式化levelCode")
    public ResultBody format() {
        try {
            return ResultBody.success(iInformationDataDirService.format());
        } catch (Exception e) {
            log.error("【格式化异常】:{}",e.getMessage());
            return ResultBody.failure("格式化异常");
        }
    }

    /**
     * 获取表的树结构
     * @return
     */
    @GetMapping("/tableTree")
    @ApiOperation(value = "获取点击表的回显树结构", notes = "获取点击表的回显树结构")
    public ResultBody tableTree(@ApiParam(value = "tableId") @RequestParam String tableId) {
        try {
            return ResultBody.success(iInformationDataDirService.getTableTree(tableId));
        } catch (Exception e) {
            log.error("【获取树形结构异常】:{}",e.getMessage());
            return ResultBody.failure("获取树形结构");
        }
    }

    /**
     * 获取表的树结构(包含数据源)
     * @return
     */
    @GetMapping("/tableTree2")
    @ApiOperation(value = "获取点击表的回显树结构", notes = "获取点击表的回显树结构")
    public ResultBody tableTree2(@ApiParam(value = "tableId") @RequestParam String tableId) {
        try {
            return ResultBody.success(iInformationDataDirService.getTableTree2(tableId));
        } catch (Exception e) {
            log.error("【获取树形结构异常】:{}",e.getMessage());
            return ResultBody.failure("获取树形结构");
        }
    }

    /**
     * 移动更改目录
     * @return
     */
    @GetMapping("/moveDir")
    @ApiOperation(value = "移动更改目录", notes = "移动更改目录")
    public ResultBody moveDir(@ApiParam(value = "要移动的目录") @RequestParam String id,
                              @ApiParam(value = "目标目录") @RequestParam String tagId,
                              @ApiParam(value = "移动位置") @RequestParam String moveType) {
        try {
            return ResultBody.success(iInformationDataDirService.moveDir(id,tagId,moveType));
        } catch (Exception e) {
            log.error("【移动目录异常】:{}",e.getMessage());
            return ResultBody.failure("移动目录失败！");
        }
    }

    /**
     * 获取所有数据源
     * @return
     */
    @GetMapping("/getDataSource")
    @ApiOperation(value = "获取所有数据源", notes = "获取所有数据源")
    public ResultBody getDataSource() {
        try {
            return ResultBody.success(iInformationDataDirService.getDataSource());
        } catch (Exception e) {
            log.error("【获取所有数据源】:{}",e.getMessage());
            return ResultBody.failure("获取所有数据源失败！");
        }
    }
}
